use lazy_static::lazy_static;
use rbase::db::DbColumnDefine;
use sea_orm::{FromQueryResult, entity::prelude::*};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Default, PartialEq, DeriveEntityModel, Deserialize, Serialize)]
#[sea_orm(table_name = "t_knowledge_progress")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i64,

    #[sea_orm(default_value = 0)]
    pub owner_id: Option<i64>,

    #[sea_orm(default_value = 0)]
    pub scheduler_id: Option<i64>,

    #[sea_orm(default_value = 0)]
    pub knowledge_id: Option<i64>,

    #[sea_orm(default_value = 0)]
    pub learned_count: Option<i64>,

    /*
       -10 : 当天需要学习
       -5  : 本周需要学习
       -1  : 本月需要学习
       0   : 未学习
       1   : 掌握1次
       2   : 掌握2次
       3   : 掌握3次
       4   : 掌握4次
       5   : 掌握5次
       10  ：完全掌握
    */
    #[sea_orm(default_value = 0)]
    pub learning_point: Option<i64>,

    #[sea_orm(default_value = 0)]
    pub created_at: Option<i64>,

    #[sea_orm(default_value = 0)]
    pub modified_at: Option<i64>,
}
pub static POINT_NEED_TO_LEARN_TODAY: i64 = -10;
pub static POINT_NEED_TO_LEARN_THIS_WEEK: i64 = -5;
pub static POINT_NEED_TO_LEARN_THIS_MONTH: i64 = -1;
pub static POINT_NOT_LEARNED: i64 = 0;
pub static POINT_MASTERED: i64 = 10;

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation, Deserialize, Serialize)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::knowledge::Entity",  // 关联到 Knowledge 实体
        from = "Column::KnowledgeId",
        to = "super::knowledge::Column::Id"
    )]
    Knowledge, // 关系名称
}

impl ActiveModelBehavior for ActiveModel {} // 必须实现这个trait

pub static TABLE_NAME: &str = "t_knowledge_progress";
pub static CREATE_QUERY: &str = r#"
    CREATE TABLE IF NOT EXISTS t_knowledge_progress (
        id              INTEGER NOT NULL PRIMARY KEY,
        owner_id        INTEGER NOT NULL,
        scheduler_id    INTEGER NOT NULL,
        knowledge_id    INTEGER NOT NULL,
        learned_count   INTEGER NOT NULL,
        learning_point  INTEGER NOT NULL,
        created_at      INTEGER NOT NULL,
        modified_at     INTEGER NOT NULL,
        UNIQUE (scheduler_id, knowledge_id)
    );
"#;

lazy_static! {
    pub static ref TABLE_COLUMNS: Vec<DbColumnDefine<'static>> = vec![];
}

pub fn get_column(name: &String) -> Column {
    match name.as_str() {
        "id" => Column::Id,
        "owner_id" => Column::OwnerId,
        "scheduler_id" => Column::SchedulerId,
        "knowledge_id" => Column::KnowledgeId,
        "learning_point" => Column::LearningPoint,
        "learned_count" => Column::LearnedCount,
        "created_at" => Column::CreatedAt,
        "modified_at" => Column::ModifiedAt,
        _ => Column::Id,
    }
}

#[derive(Debug, FromQueryResult, Serialize)]
pub struct SearchResult {
    pub id: i64,

    pub scheduler_id: Option<i64>,

    pub knowledge_id: Option<i64>,

    pub learned_count: Option<i64>,

    pub learning_point: Option<i64>,

    pub created_at: Option<i64>,

    pub modified_at: Option<i64>,

    // 关联表 knowledge 的 name 字段
    #[sea_orm(column_name = "knowledge.name")]
    pub name: Option<String>,
}
